package com.honghu.villagepost.widget;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.honghu.villagepost.R;
import com.honghu.villagepost.adapter.ItemTypeAdapter;
import com.honghu.villagepost.entity.Select;
import com.blankj.utilcode.util.ToastUtils;
import com.honghu.villagepost.adapter.ItemTypeAdapter;
import com.lxj.xpopup.core.BottomPopupView;

/**
 * @author: mqwang
 * @create: 2/20/21 11:36 AM
 * @Description:
 */
public class ItemTypeSelectPopup extends BottomPopupView {

    private PopCallBack callBack;
    private View btnSkuQuantityMinus;
    private TextView etSkuQuantityInput;
    private TextView btnSkuQuantityPlus;

    public interface PopCallBack {
        void showValue(String goodsInfo, String weight);
    }

    public ItemTypeSelectPopup(@NonNull Context context, PopCallBack callBack) {
        super(context);
        this.callBack = callBack;
    }

    @Override
    protected int getImplLayoutId() {
        return R.layout.popup_item_type_select;
    }

    @Override
    protected void onCreate() {
        super.onCreate();

        RecyclerView recyclerView = findViewById(R.id.recycler_view);
        recyclerView.setLayoutManager(new GridLayoutManager(getContext(), 3));
        ItemTypeAdapter itemTypeAdapter = new ItemTypeAdapter();
        recyclerView.setAdapter(itemTypeAdapter);
        itemTypeAdapter.setOnItemClickListener((adapter, view, position) -> {
            itemTypeAdapter.selectItem(position);
        });

        itemTypeAdapter.addData(new Select(false, "汽配"));
        itemTypeAdapter.addData(new Select(false, "手机配件"));
        itemTypeAdapter.addData(new Select(false, "食品"));
        itemTypeAdapter.addData(new Select(false, "其他"));

        btnSkuQuantityMinus = findViewById(R.id.btn_sku_quantity_minus);
        etSkuQuantityInput = findViewById(R.id.et_sku_quantity_input);
        btnSkuQuantityPlus = findViewById(R.id.btn_sku_quantity_plus);

        btnSkuQuantityMinus.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String quantity = etSkuQuantityInput.getText().toString();
                if (TextUtils.isEmpty(quantity)) {
                    return;
                }
                int quantityInt = Integer.parseInt(quantity);
                if (quantityInt > 1) {
                    String newQuantity = String.valueOf(quantityInt - 1);
                    etSkuQuantityInput.setText(newQuantity);
                }
            }
        });

        btnSkuQuantityPlus.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String quantity = etSkuQuantityInput.getText().toString();
                if (TextUtils.isEmpty(quantity) ) {
                    return;
                }
                int quantityInt = Integer.parseInt(quantity);
                quantityInt ++ ;
                etSkuQuantityInput.setText(quantityInt+"");
            }
        });

        findViewById(R.id.tv_confirm).setOnClickListener(v -> {
            String goodsInfo = "";
            for (Select select :itemTypeAdapter.getData()){
                if (select.isSelect()){
                    goodsInfo = select.getName();
                }
            }
            String weight = etSkuQuantityInput.getText().toString();
            if (TextUtils.isEmpty(weight)){
                ToastUtils.showShort("请选择物品类型");
                return;
            }
            if (callBack != null){
                callBack.showValue(goodsInfo, weight);
            }
            dismiss();
        });
    }

}
